﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA.Forms
{
    public partial class Form_AlarmRecent : fdBaseForm
    {

        string[][] his_AlarmTitle = new string[5][] {
            new string[3]{ "序号","ID","" },
            new string[3]{ "警报名称", "Alarm Name","" },
            new string[3]{ "警报等级", "Alarm Level","" },
            new string[3]{ "发生时间", "Occur time","" },
            new string[3]{ "复位时间", "Reset time","" }
            };

        public Form_AlarmRecent()
        {
            InitializeComponent();
        }

        private void Form_AlarmRecent_Load(object sender, EventArgs e)
        {
            InitAlarmTable();
            LoadAlarmData(DateTime.Now);
            if (lst_Alarm.Items.Count < 100) LoadAlarmData(DateTime.Now.AddMonths(-1));
        }


        private void InitAlarmTable()
        {
            this.Size = new Size(netForms.fmAlarm.Width - 120, netForms.fmAlarm.Height - 300);
            this.Location = new Point(60, 150);
            lst_Alarm.Columns.Add(his_AlarmTitle[0][MultipleLanguage.systemLanguageIndex], 100, HorizontalAlignment.Left);
            lst_Alarm.Columns.Add(his_AlarmTitle[1][MultipleLanguage.systemLanguageIndex], lst_Alarm.Width - 622, HorizontalAlignment.Center);
            lst_Alarm.Columns.Add(his_AlarmTitle[2][MultipleLanguage.systemLanguageIndex], 100, HorizontalAlignment.Left);
            lst_Alarm.Columns.Add(his_AlarmTitle[3][MultipleLanguage.systemLanguageIndex], 200, HorizontalAlignment.Left);
            lst_Alarm.Columns.Add(his_AlarmTitle[4][MultipleLanguage.systemLanguageIndex], 200, HorizontalAlignment.Left);
        }
        private void LoadAlarmData(DateTime datetime)
        {

            DataTable dt = DBOperate.his_GetAlarmValues(datetime.AddDays(-7), datetime);

            if (dt == null || dt.Rows.Count == 0) return;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!int.TryParse(dt.Rows[i]["AlarmName"].ToString(), out int x)) continue;
                Alarm al = NetLine.alarm.Find(v=>v.AlarmID== x);
                if (al == null) continue;   
                if (dt.Rows[i]["AlarmActive"].ToString() == "0")
                {
                    lst_Alarm.Items.Add(new ListViewItem(new string[] { (lst_Alarm.Items.Count + 1).ToString(), al.AlarmName[MultipleLanguage.systemLanguageIndex], dt.Rows[i]["AlarmLevel"].ToString(), "", dt.Rows[i]["SaveTime"].ToString() }));//最后是报警等级，最后修改
                }
                else
                {
                    //1这里加载数据，对数据进行重组
                    int lastindex = -10;
                    for (int k = lst_Alarm.Items.Count - 1; k >= 0; k--)
                    {
                        if (dt.Rows[i]["AlarmName"].ToString() == lst_Alarm.Items[k].SubItems[1].Text && lst_Alarm.Items[k].SubItems[3].Text == "")
                        {
                            lst_Alarm.Items[k].SubItems[3].Text = dt.Rows[i]["SaveTime"].ToString();
                            lastindex = k;
                            break;
                        }
                    }
                    if (lastindex == -10)
                    {
                        lst_Alarm.Items.Add(new ListViewItem(new string[] { (lst_Alarm.Items.Count + 1).ToString(), al.AlarmName[MultipleLanguage.systemLanguageIndex], dt.Rows[i]["AlarmLevel"].ToString(), dt.Rows[i]["SaveTime"].ToString(), "" }));
                    }
                }
            }
        }

        private void btn_AlarmRecentClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
